package com.zhoudb.blog.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Description:七牛云的工具类
 * @Author: zhoudb
 * @Date: 2018-10-09 16:43
 */
public class QiniuUtil {

    public static final String preUrl = "pgbppwnor.bkt.clouddn.com"; //七牛云给的空间的前缀地址
    private static final String accessKey = "I2DY2fOUY_pcxk8syRn39_OxihEec-yp_tImA2-s";
    private static final String secretKey = "MGlV7SI8FAkxWZFzNvaGDlsWpQnIH2Qw49MPpIUB";
    private static final String bucket = "zhoudb"; //空间名

    /**
     * 功能描述:上传到七牛云
     * @param file
     * @return
     * @author zhoudb
     * @date 2018/10/9 16:53
     */
    public static String upload(MultipartFile file) {
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        UploadManager uploadManager = new UploadManager(cfg);
        String key = UUID.randomUUID().toString().replaceAll("-", "");
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        String res = null;
        try {
            Response response = uploadManager.put(file.getBytes(), key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            res = putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            ex.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }


    /**
     * 功能描述: 获取图片列表
     * @param
     * @return
     * @author zhoudb
     * @date 2018/10/9 16:53
     */
    public static void getImgList() {
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        //文件名前缀
        String prefix = "";
        //每次迭代的长度限制，最大1000，推荐值 1000
        int limit = 1000;
        //指定目录分隔符，列出所有公共前缀（模拟列出目录效果）。缺省值为空字符串
        String delimiter = "";
        //列举空间文件列表
        BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
        while (fileListIterator.hasNext()) {
            //处理获取的file list结果
            FileInfo[] items = fileListIterator.next();
            for (FileInfo item : items) {
                System.out.println(item.key);
                System.out.println(item.hash);
                System.out.println(item.fsize);
                System.out.println(item.mimeType);
                System.out.println(item.putTime);
                System.out.println(item.endUser);
            }
        }
    }

}
